<?php
header('Content-disposition: attachment; filename='.$_GET['stn'].'_'.date("Y-m-d", time()-86400).'.csv');
header("Content-type: text/csv");
header("Pragma: no-cache");
header("Expires: 0");
mysql_connect('localhost', 'root', '');
mysql_select_db('unwdmi');
include('../inc/functions.inc.php');

$sql = "SELECT s.stn AS stn, name, country, latitude ,longitude, elevation,
					AES_DECRYPT(temp,'@#dwave\$r') AS temp,
						AES_DECRYPT(dewp,'@#dwave\$r') AS dewp,
						AES_DECRYPT(stp,'@#dwave\$r') AS stp,
						AES_DECRYPT(slp,'@#dwave\$r') AS slp,
						AES_DECRYPT(visib,'@#dwave\$r') AS visib,
						AES_DECRYPT(wdsp,'@#dwave\$r') AS wdsp,
						AES_DECRYPT(prcp,'@#dwave\$r') AS prcp,
						AES_DECRYPT(sndp,'@#dwave\$r') AS sndp,
						AES_DECRYPT(frshtt,'@#dwave\$r') AS frshtt,
						AES_DECRYPT(cldc,'@#dwave\$r') AS cldc,
						AES_DECRYPT(wnddir,'@#dwave\$r') AS wnddir, d.date AS date,
						AES_DECRYPT(maxtemp,'@#dwave\$r') AS maxtemp,
					FROM stations AS s, dailyhistory AS d
					WHERE s.stn = d.stn
					AND s.stn = ".$_GET['stn']."
					AND d.date = DATE_SUB(CURDATE(),INTERVAL 1 DAY)
					ORDER BY d.date DESC";
$result = mysql_query($sql);

echo "Station ID;Name;Country;Latitude;Longitude;Elevation;Date;Temperature;Dewpoint;Station Level Pressure;Sea Level Pressure; Visibility;Windspeed;Precipitation;Snowfall;Events;Cloud Coverage;Winddirection;Peak Temperature\n";

if($row = mysql_fetch_assoc($result)) {
			echo $row['stn'].';';
			echo $row['name'].';';
			echo $row['country'].';';
			echo $row['latitude'].';';
			echo $row['longitude'].';';
			echo $row['elevation'].';';
			echo $row['date'].';';
			echo $row['temp'].';';
			echo $row['dewp'].';';
			echo $row['stp'].';';
			echo $row['slp'].';';
			echo $row['visib'].';';
			echo $row['wdsp'].';';
			echo $row['prcp'].';';
			echo $row['sndp'].';';
			echo readFRSHTTnoHTML($row['frshtt']).';';
			echo $row['cldc'].';';
			echo $row['wnddir'].';';
			echo $row['maxtemp'];
}
else {
	echo 'No data found';
}
?>